from django.db import models
from utils.models import BaseModel
from user.models import UserInfo
from classify.models import Category


class GoodsShelf(BaseModel):
    goods_name = models.CharField(max_length=64, verbose_name='商品名')
    goods_intro = models.CharField(max_length=288, verbose_name='商品简介')
    goods_img = models.ImageField(upload_to='goods_img', verbose_name='商品图片')
    goods_detail = models.TextField(verbose_name='商品详情')
    original_price = models.IntegerField(verbose_name='商品价格')
    selling_price = models.IntegerField(verbose_name='商品实际售价')
    stock_num = models.IntegerField(verbose_name='商品库存数量')
    tag = models.CharField(max_length=32, verbose_name='商品标签')
    sales_num = models.IntegerField(default=0, verbose_name='销出数量')

    create_user = models.ForeignKey(to=UserInfo, related_name='create_goods', on_delete=models.CASCADE,
                                    db_constraint=False, verbose_name='添加用户')
    update_user = models.ForeignKey(to=UserInfo, related_name='update_goods', on_delete=models.CASCADE,
                                    db_constraint=False, verbose_name='修改用户')
    goods_classify = models.ForeignKey(to=Category, related_name='goods', on_delete=models.CASCADE,
                                       db_constraint=False, verbose_name='分类')

    class Meta:
        db_table = 'shop_goods'
        verbose_name_plural = '商品表'

    def __str__(self):
        return self.goods_name

    @property
    def goods_banner(self):
        good_banner = []
        for item in self.goodsbanner.all():
            good_banner.append({
                'id': item.id,
                'banner_name': item.name,
                'goods_banner': 'http://127.0.0.1:8000/media/' + str(item.goods_banner)
            })
        return good_banner

    @property
    def goods_ad(self):
        good_ad = []
        for item in self.goodsad.all():
            good_ad.append({
                'id': item.id,
                'ad_name': item.name,
                'goods_ad': 'http://127.0.0.1:8000/media/' + str(item.goods_ad)
            })
        return good_ad

    @property
    def goods_arguments(self):
        good_arguments = []
        for item in self.goodsarguments.all():
            good_arguments.append({
                'goods_cdma': item.cdma,
                'goods_brand': item.brand,
                'goods_model': item.model,
                'goods_resolution_ratio': item.resolution_ratio,
                'goods_color': item.color,
                'goods_combo': item.combo,
                'goods_storage': item.storage,
                'goods_time_to_market': item.time_to_market,
                'goods_processor': item.processor,
                'goods_battery_capacity': item.battery_capacity,
                'goods_pixel': item.pixel,
                'goods_certificate': item.certificate,
            })
        return good_arguments


class GoodsBanner(BaseModel):
    name = models.CharField(verbose_name='商品轮播图', max_length=32)
    goods_banner = models.ImageField(upload_to='goods_banner', verbose_name='轮播图片')
    info = models.CharField(max_length=256, verbose_name='商品轮播图详情')

    goods = models.ForeignKey(to=GoodsShelf, on_delete=models.CASCADE, related_name='goodsbanner',
                              db_tablespace=False, verbose_name='商品关联字段')

    class Meta:
        db_table = 'shop_goodsbanner'
        verbose_name_plural = '商品轮播图表'

    def __str__(self):
        return self.name


class GoodsAD(BaseModel):
    name = models.CharField(verbose_name='商品广告', max_length=32)
    goods_ad = models.ImageField(upload_to='goods_ad', verbose_name='广告图片')
    info = models.CharField(max_length=256, verbose_name='广告图详情')

    goods = models.ForeignKey(to=GoodsShelf, on_delete=models.CASCADE, related_name='goodsad',
                              db_tablespace=False, verbose_name='商品关联字段')

    class Meta:
        db_table = 'shop_goodsad'
        verbose_name_plural = '商品广告表'

    def __str__(self):
        return self.name


class GoodsArguments(models.Model):
    cdma = models.CharField(max_length=32, verbose_name='网络模型', null=True, blank=True)
    brand = models.CharField(max_length=32, verbose_name='品牌', null=True, blank=True)
    model = models.CharField(max_length=32, verbose_name='型号', null=True, blank=True)
    resolution_ratio = models.CharField(max_length=32, verbose_name='分辨率', null=True, blank=True)
    color = models.CharField(max_length=254, verbose_name='机身颜色', null=True, blank=True)
    combo = models.CharField(max_length=254, verbose_name='套餐类型', null=True, blank=True)
    storage = models.CharField(max_length=254, verbose_name='存储容量', null=True, blank=True)
    time_to_market = models.DateField(auto_now=True, verbose_name='上市时间', null=True, blank=True)
    processor = models.CharField(max_length=24, verbose_name='处理器', null=True, blank=True)
    screen_size = models.CharField(max_length=24, verbose_name='屏幕尺寸', null=True, blank=True)
    charge_power = models.CharField(max_length=24, verbose_name='充电功率', null=True, blank=True)
    battery_capacity = models.CharField(max_length=24, verbose_name='电池容量', null=True, blank=True)
    pixel = models.CharField(max_length=24, verbose_name='像素', null=True, blank=True)
    certificate = models.CharField(max_length=56, verbose_name='3c证书', null=True, blank=True)

    good = models.ForeignKey(to=GoodsShelf, related_name='goodsarguments', on_delete=models.CASCADE,
                             db_tablespace=False, verbose_name='商品关联字段')

    class Meta:
        db_table = 'shop_goods_arguments'
        verbose_name_plural = '商品广告表'

    def __str__(self):
        return self.brand


class GoodsParameters(models.Model):
    color = models.CharField(max_length=64, verbose_name='机身颜色', null=True, blank=True)
    storage = models.CharField(max_length=64, verbose_name='存储容量', null=True, blank=True)
    net_work = models.CharField(max_length=64, verbose_name='网络类型', null=True, blank=True)
    combo = models.CharField(max_length=64, verbose_name='套餐类型', null=True, blank=True)

    goods = models.ForeignKey(to=GoodsShelf, related_name='goodsparameters', on_delete=models.CASCADE,
                              db_tablespace=False, verbose_name='商品关联表')


    class Meta:
        db_table = 'shop_goods_parameters'
        verbose_name_plural = '商品单独参数表'

    def __str__(self):
        return self.color
